# -*- coding: utf-8 -*-
# author: sixmillions
# date: 2022/3/21


import numpy as np
import pandas as pd

# python的list创建Series
a = pd.Series(['A', 'B', 'C', 'D', 'E'])
print(a)
print(type(a))  # <class 'pandas.core.series.Series'>
print(a.values, type(a.values))  # ['A' 'B' 'C' 'D' 'E'] <class 'numpy.ndarray'>
print(a.index, type(a.index))  # RangeIndex(start=0, stop=5, step=1) <class 'pandas.core.indexes.range.RangeIndex'>
print(a.index.values)  # [0 1 2 3 4]
print("---------------------------")

# numpy的array创建
b = pd.Series(np.arange(1, 10))
print(b, type(b))
print("---------------------------")

# 通过字典创建
c = pd.Series({1: 'A', 2: 'B', 3: 'C', 4: 'D'})
print(c)
print(c.index)  # Int64Index([1, 2, 3, 4], dtype='int64')
print(c.values)  # ['A' 'B' 'C' 'D']
print("---------------------------")

# 分别指定values和index
d = pd.Series(['a', 'b', 'c', 'd'], index=[1, 2, 3, 4])
print(d)
print(d.index)  # Int64Index([1, 2, 3, 4], dtype='int64')
print(d.values)  # ['a' 'b' 'c' 'd']
print("---------------------------")

# ####
# 访问操作
# ####
# 通过索引访问
print(d[2])  # b
# 获取值是'a'的
d1 = d[d == 'a']
print(d1)  # 1    a
print(type(d1))  # <class 'pandas.core.series.Series'>

e = pd.Series([1, 2, 3, 4, 5], index=['A', 'B', 'C', 'D', 'E'])
# 通过索引访问
print(e['B'])  # 2
e1 = e > 3
print(e1, type(e1))  # dtype: bool <class 'pandas.core.series.Series'>
print(e[e1])

# Series转化为字典
print(e.to_dict())  # {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}

# 单独指定index
my_index = ['A', 'B', 'C', 'D', 'E', 'F']
f = pd.Series(e, index=my_index)
print(f)
print(pd.isnull(f))
print(pd.notnull(f))

# 指定名字
e.name = 'hello'
e.index.name = 'hello index'
print(e)
